//
//  YXForumViewController.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/24.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXForumViewController.h"
#import "YXForumTableView.h"
#import "YXForumToolbarView.h"
#import "YXReleaseViewController.h"
#import "YXTopicViewController.h"
#import "YXReplyViewController.h"
#import "YXPostModel.h"
@interface YXForumViewController ()
@property (nonatomic ,strong) YXForumTableView *tableView;
@property (nonatomic ,strong) YXForumToolbarView *toolBarView;
@property (nonatomic ,strong) NSMutableArray *listArr;
@property (nonatomic ,strong) NSString *page;
@end

@implementation YXForumViewController

- (NSMutableArray *)listArr {
    if (!_listArr) {
        _listArr = [NSMutableArray array];
    }
    return _listArr;
}

- (YXForumTableView *)tableView {
    if (!_tableView) {
        _tableView = [[YXForumTableView alloc] initWithFrame:(CGRectMake(0, -64, kScreenWidth, kScreenHeight-50)) style:(UITableViewStylePlain)];
        YXWeakSelf
        // 点击回复
        [_tableView setClickReplyBtnBlock:^(NSString *ID){
            YXReplyViewController *replyVC = [[YXReplyViewController alloc] init];
            replyVC.ID = ID;
            [weakSelf.navigationController pushViewController:replyVC animated:YES];
        }];
       
        // 点击收藏
        [_tableView setClickCollerBtnBlock:^(UIButton *btn,YXPostModel *model) {
            
            model.collect = !model.isCollect;
            if (model.isCollect) {
                //                http://121.196.210.134:8082/mpos/wx/addCollect.do?sessionId=c517b5c1-4352-4c20-a336-5fb4ad31a24f&post_id=1
                NSString *urlStr = @"addCollect.do";
                NSDictionary *parameter = @{@"sessionId":[YXUserInfoManager getUserInfo].sessionId,@"post_id":model.ID};
                [BX_NetWorkManager requestForPOSTWithUrl:urlStr parameter:parameter success:^(id responseObject) {
                    [weakSelf showTostMessage:@"已收藏"];
                    
                    NSInteger number = [model.collectCount integerValue];
                    number+=1;
                    NSString *str = [NSString stringWithFormat:@"%ld",number];
                    [btn setTitle:str forState:(UIControlStateNormal)];
                    
                } failure:^(NSError *error) {
                    NSLog(@"%@",error);
                    [weakSelf showAlertWithTitle:@"提示" Messge:@"维护中，请稍等！"];
                }];
                
            }else {
               
                NSString *urlStr = @"cancelCollect.do";
                NSDictionary *parameter = @{@"sessionId":[YXUserInfoManager getUserInfo].sessionId,@"post_id":model.ID};
                [BX_NetWorkManager requestForPOSTWithUrl:urlStr parameter:parameter success:^(id responseObject) {
                    [weakSelf showTostMessage:@"已取消"];
                    if ([model.collectCount integerValue] > 1) {
                        NSInteger number = [model.collectCount integerValue];
                        number-=1;
                        NSString *str = [NSString stringWithFormat:@"%ld",number];
                        [btn setTitle:str forState:(UIControlStateNormal)];
                    }
                    
                } failure:^(NSError *error) {
                    NSLog(@"%@",error);
                    [weakSelf showAlertWithTitle:@"提示" Messge:@"维护中，请稍等！"];
                }];
            }
            
            [weakSelf.tableView reloadData];
        }];
      
        // 点击跳转话题
        [_tableView setSelectRowAtIndexPath:^(NSString *ID) {
            YXTopicViewController *topicVC = [[YXTopicViewController alloc] init];
            topicVC.ID = ID;
            [weakSelf.navigationController pushViewController:topicVC animated:YES];
        }];
        
    }
    return _tableView;
}

- (YXForumToolbarView *)toolBarView {
    if (!_toolBarView) {
        _toolBarView = [[YXForumToolbarView alloc] initWithFrame:(CGRectMake(0, kScreenHeight-64-50, kScreenWidth, 50))];
        YXWeakSelf
        [_toolBarView setClickSelectBtnBlock:^(NSInteger index,UIButton *button) {
            switch (index) {
                case 0:    // 置顶
                {
                    [weakSelf.tableView setContentOffset:CGPointMake(0,0) animated:YES]; 
                    
                }
                    break;
                case 1:    // 刷新 请求数据
                {
                 
                    CABasicAnimation *roration = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
                    [roration setToValue:@(M_PI*2)];
                    [roration setDuration:0.8];
                    [roration setRepeatCount:2];
                    [button.layer addAnimation:roration forKey:@"rotation"];
                    
                    [weakSelf.tableView reloadData];
                    
                }
                    break;
                case 2:    // 发布
                {
                    
//                    NSMutableAttributedString *attTitle = [[NSMutableAttributedString alloc]initWithString:@"" attributes:@{NSForegroundColorAttributeName:color_textOne,NSFontAttributeName:kFont(19)}];
                    
                    UIAlertController *action = [UIAlertController alertControllerWithTitle:@"发布" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//                    [action setValue:attTitle forKey:@"attributedTitle"];
                    
                    
                    UIAlertAction *alert1 = [UIAlertAction actionWithTitle:@"图文论坛" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                        YXReleaseViewController *resVC = [[YXReleaseViewController alloc] init];
                        resVC.typeView = Release_Graphic;
                        [weakSelf.navigationController pushViewController:resVC animated:YES];
                 
                    }];
                    UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"视频论坛" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                        YXReleaseViewController *resVC = [[YXReleaseViewController alloc] init];
                        resVC.typeView = Release_Video;
                        [weakSelf.navigationController pushViewController:resVC animated:YES];
                    }];
                    
                    [alert1 setValue:color_textOne forKey:@"titleTextColor"];
                    [alert2 setValue:color_textOne forKey:@"titleTextColor"];

                    [action addAction:alert1];
                    [action addAction:alert2];
                    
                    UIAlertAction *can = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                        
                    }];
                    [can setValue:color_textOne forKey:@"titleTextColor"];
                    [action addAction:can];
                    [weakSelf presentViewController:action animated:YES completion:nil];
                }
                    break;
                default:
                    break;
            }
            
        }];
    }
    return _toolBarView;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"论坛";
    self.page = @"1";
    [self requestDataList];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.toolBarView];
    [self addRefresh];

//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"导航栏_002"] style:(UIBarButtonItemStyleDone) target:self action:@selector(leftBarButtonItem)];
//    self.navigationItem.leftBarButtonItem = leftItem;
  
}

//- (void)leftBarButtonItem {
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (void)requestDataList {
//    http://121.196.210.134:8082/test_mpos/wx/postList.do?page=1&rows=10&sessionId=20457425-0477-45b5-a941-32c8e762a501
    NSString *urlStr = @"postList.do";
    NSDictionary *parameter = @{@"page":self.page,@"rows":@"10",@"sessionId":[YXUserInfoManager getUserInfo].sessionId};
    [BX_NetWorkManager requestForPOSTWithUrl:urlStr parameter:parameter success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        if (REQUESTDATASUCCESS) {
            [self.tableView.mj_footer endRefreshing];
            NSArray *listArr = [YXPostModel mj_objectArrayWithKeyValuesArray:responseObject[@"body"][@"postInfo"][@"list"]];
            [self.listArr addObjectsFromArray:listArr];
            self.tableView.listArr = self.listArr;
            if (self.listArr.count == [responseObject[@"body"][@"postInfo"][@"totalCount"] integerValue]) {
                self.tableView.mj_footer.hidden = YES;
            }else {
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshing];
            }
        }

    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [self.tableView.mj_footer endRefreshing];
        [self showAlertWithTitle:@"提示" Messge:@"维护中，请稍等！"];
    }];
    
}

- (void)addRefresh {
    NSInteger pageNo = [self.page integerValue];
    pageNo ++;
    self.page = [NSString stringWithFormat:@"%ld",pageNo];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)];
}

- (void)loadMoreTopics {
    [self requestDataList];
    [self.tableView.mj_footer endRefreshing];

}

@end
